package com.iplant.wms.serviceImpl.dao.pkg;

import com.iplant.base.po.OutResult;
import com.iplant.base.po.Pagination;
import com.iplant.base.po.enums.ExceptionTypes;
import com.iplant.base.utils.general.StringUtils;
import com.iplant.base.utils.sql.BaseDAO;
import com.iplant.basic.service.po.bms.BMSEmployee;
import com.iplant.wms.service.po.pkg.MSSContainer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

public class MSSContainerDAO extends BaseDAO {

	private static MSSContainerDAO Instance = null;
	private static Logger logger = LoggerFactory.getLogger(MSSContainerDAO.class);

	private MSSContainerDAO() {
		super();

		setTableName("mss_container");
	}

	public static MSSContainerDAO getInstance() {
		if (Instance == null)
			Instance = new MSSContainerDAO();
		return Instance;
	}

	public void MSS_UpdateContainer(BMSEmployee wLoginUser, MSSContainer wContainer, OutResult<Integer> wErrorCode) {
		wErrorCode.set(0);

		try {
			if (wContainer == null || wContainer.TypeID <= 0 || StringUtils.isEmpty(wContainer.Name)) {
				wErrorCode.set(ExceptionTypes.Parameter.getValue());
				return;
			}

			MSSContainer wContainerO = this.MSS_CheckContainer(wLoginUser, wContainer, wErrorCode);
			if (wContainerO.ID > 0) {
				wErrorCode.set(ExceptionTypes.Duplication.getValue());
			}
			if (wErrorCode.Result != 0)
				return;

			Map<String, Object> wParwms = new HashMap<String, Object>();

			wParwms.put("Name", wContainer.Name);
			wParwms.put("Code", wContainer.Code);
			wParwms.put("Description", wContainer.Description);
			wParwms.put("TypeID", wContainer.TypeID);

			wParwms.put("EditorID", wLoginUser.getID());
			wParwms.put("EditTime", Calendar.getInstance());
			wParwms.put("Active", wContainer.Active);

			if (wContainer.ID > 0) {
				wParwms.put("ID", wContainer.ID);
				super.Update(super.getTableName("mss_container"), "ID", wParwms);
			} else {
				wParwms.put("CreatorID", wLoginUser.getID());
				wParwms.put("CreateTime", Calendar.getInstance());
				wContainer.ID = super.Insert(super.getTableName("mss_container"), wParwms);
			}

		} catch (Exception ex) {
			wErrorCode.set(ExceptionTypes.DBSQL.getValue());
			logger.error("MSS_UpdateContainer", ex);
		}
	}

	public void MSS_ActiveContainer(BMSEmployee wLoginUser, MSSContainer wContainer, int wActive,
			OutResult<Integer> wErrorCode) {
		wErrorCode.set(0);
		// 判断客户信息是否存在(中国：统一社会信用代码，国外:提醒是否有重复）

		try {
			if (wContainer == null || wContainer.ID <= 0) {
				wErrorCode.set(ExceptionTypes.Parameter.getValue());
				return;
			}

			Map<String, Object> wParwms = new HashMap<String, Object>();

			wParwms.put("ID", wContainer.ID);
			wParwms.put("EditorID", wLoginUser.getID());
			wParwms.put("EditTime", Calendar.getInstance());
			wParwms.put("Active", wActive);
			super.Update(super.getTableName("mss_container"), "ID", wParwms);

		} catch (Exception ex) {
			wErrorCode.set(ExceptionTypes.DBSQL.getValue());

			logger.error("MSS_ActiveContainer", ex);
		}
	}

	public List<MSSContainer> MSS_QueryContainerList(BMSEmployee wLoginUser, int wTypeID, String wName,
			String wTypeCode, String wTypeName, int wActive, Pagination wPagination, OutResult<Integer> wErrorCode) {
		return this.MSS_QueryContainerList(wLoginUser, -1, "", wTypeID, wName, wTypeCode, wTypeName, wActive,
				wPagination, wErrorCode);
	}

	private List<MSSContainer> MSS_QueryContainerList(BMSEmployee wLoginUser, int wID, String wCode, int wTypeID,
			String wName, String wTypeCode, String wTypeName, int wActive, Pagination wPagination,
			OutResult<Integer> wErrorCode) {
		List<MSSContainer> wMSSContainerList = new ArrayList<MSSContainer>();
		wErrorCode.set(0);

		try {

			// Step02:查询
			Map<String, Object> wParwms = new HashMap<String, Object>();
			String wSQLText = "";
			wSQLText = StringUtils.Format("Select t.*,t2.Name as TypeName,t2.Code as TypeCode,t2.Length,t2.Width,"
					+ " t2.Height,t2.Weight,t2.Bearing,t2.Essence,t2.MinPackingNumber,t3.Name as CreatorName ,t4.Name as EditorName"
					+ " from {0}.mss_container t left join {0}.mss_containertype t2 on t.TypeID=t2.ID "
					+ " left join {0}.mbs_user t3 on t.CreatorID=t3.ID "
					+ " left join {0}.mbs_user t4 on t.EditorID=t4.ID "
					+ " where (:ID<=0 or  t.ID=:ID) and  (:TypeID<=0 or  t.TypeID=:TypeID)"
					+ " AND (:Active<0 or  t.Active=:Active) and (:Code =''''  or  t.Code=:Code)"
					+ " AND (:TypeCode =''''  or  t2.Code = :TypeCode)"
					+ " AND (:TypeName =''''  or  t2.Name like :TypeName)"
					+ " AND (:Name = '''' or  t.Name like :Name)", super.getDBName());

			wSQLText = this.DMLChange(wSQLText);

			wParwms.put("ID", wID);
			wParwms.put("Code", wCode);
			wParwms.put("Active", wActive);
			wParwms.put("TypeID", wTypeID);
			wParwms.put("TypeCode", wTypeCode);
			wParwms.put("TypeName", StringUtils.isEmpty(wTypeName) ? "" : ("%" + wTypeName + "%"));
			wParwms.put("Name", StringUtils.isEmpty(wName) ? "" : ("%" + wName + "%"));

			wMSSContainerList = super.QueryForList(wSQLText, wParwms, wPagination, MSSContainer.class);

			// Step03:配置系统
		} catch (Exception ex) {
			wErrorCode.set(ExceptionTypes.DBSQL.getValue());
			logger.error("MSS_QueryContainerList", ex);
		}
		return wMSSContainerList;
	}

	public MSSContainer MSS_QueryContainer(BMSEmployee wLoginUser, int wID, String wCode,
			OutResult<Integer> wErrorCode) {
		MSSContainer wResult = new MSSContainer();
		wErrorCode.set(0);

		try {
			List<MSSContainer> wList = null;
			if (wID > 0) {
				wList = this.MSS_QueryContainerList(wLoginUser, wID, "", -1, "", "", "", -1, Pagination.getDefault(),
						wErrorCode);

			} else if (StringUtils.isNotEmpty(wCode)) {
				wList = this.MSS_QueryContainerList(wLoginUser, -1, wCode, -1, "", "", "", -1, Pagination.getDefault(),
						wErrorCode);
			}
			if (wList != null && wList.size() > 0) {
				wResult = wList.get(0);
			}
		} catch (Exception ex) {
			wErrorCode.set(ExceptionTypes.DBSQL.getValue());
			logger.error("MSS_QueryContainerByID", ex);
		}
		return wResult;
	}

	public MSSContainer MSS_CheckContainer(BMSEmployee wLoginUser, MSSContainer wMSSContainer,
			OutResult<Integer> wErrorCode) {
		MSSContainer wContainer = new MSSContainer();
		wErrorCode.set(0);

		try {

			// Step0:查询
			Map<String, Object> wParwms = new HashMap<String, Object>();
			String wSQLText = StringUtils.Format("Select * from {0}.mss_container " + " where ID!=:ID and  Code=:Code",
					super.getDBName());
			wParwms.clear();
			wParwms.put("ID", wMSSContainer.ID);
			wParwms.put("Name", wMSSContainer.Name);
			wParwms.put("Code", wMSSContainer.Code);

			wSQLText = this.DMLChange(wSQLText);
			List<Map<String, Object>> wQueryResultList = super.queryForList(wSQLText, wParwms);
			for (Map<String, Object> wSqlDataReader : wQueryResultList) {
				wContainer.ID = StringUtils.parseInt(wSqlDataReader.get("ID"));
				wContainer.Name = StringUtils.parseString(wSqlDataReader.get("Name"));
				wContainer.Code = StringUtils.parseString(wSqlDataReader.get("Code"));

				wContainer.CreatorID = StringUtils.parseInt(wSqlDataReader.get("CreatorID"));
				wContainer.CreateTime = StringUtils.parseCalendar(wSqlDataReader.get("CreateTime"));
				wContainer.EditorID = StringUtils.parseInt(wSqlDataReader.get("EditorID"));
				wContainer.EditTime = StringUtils.parseCalendar(wSqlDataReader.get("EditTime"));
				wContainer.Active = StringUtils.parseInt(wSqlDataReader.get("Active"));

			}

		} catch (Exception ex) {
			wErrorCode.set(ExceptionTypes.DBSQL.getValue());
			logger.error("MSS_CheckContainer", ex);
		}
		return wContainer;
	}

}
